﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;

namespace wt.wine.Application.ManageOrderTable.Command
{
    public class OrdercallbackCommand : IRequest<Json<string>>
    {
        /// <summary>
        /// 设备名称
        /// </summary>
        public string device { get; set; }
        /// <summary>
        /// 状态： 0、离线    1、出酒完成  2、暂停    3设备待出酒    100设备首次连接   101设备所有上行回调
        /// </summary>
        public int state { get; set; }
        /// <summary>
        /// 出酒量
        /// </summary>
        public int oow { get; set; }
    }

    public class OrdercallbackCommandHandler : IRequestHandler<OrdercallbackCommand, Json<string>>
    {
        private readonly IDevicetableRepository _idevicetableRepository;
        private readonly IordertableRepository _iordertableRepository;
        public OrdercallbackCommandHandler(IDevicetableRepository idevicetableRepository, IordertableRepository iordertableRepository)
        {
            _idevicetableRepository = idevicetableRepository;
            _iordertableRepository = iordertableRepository;
        }
        public async Task<Json<string>> Handle(OrdercallbackCommand request, CancellationToken cancellationToken)
        {
            Console.WriteLine($"回调参数：时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}, device:{request.device},state:{request.state},oow:{request.oow}");
            if (request.state == 1 || request.state == 2 || request.state==3 || request.state==0 || request.state==100 || request.state == 101)
            {
                if (!string.IsNullOrWhiteSpace(request.device))
                {
                    var device = await _idevicetableRepository.GetDevicetableByid(request.device, cancellationToken);
                    if (device != null && device.id > 0)
                    {
                        /// 状态： 0、离线    1、出酒完成  2、暂停    3设备待出酒    100设备首次连接   101 所有上行命令回调
                        /// ：-1 删除、0 离线 、1 在线且空闲 2 在线出酒暂停 3、在线等待出酒
                        switch (request.state)
                        {
                            case 0: //离线
                                device.state = 0;
                                break;
                            case 1: //出酒完成
                            case 100: //设备首次连接
                            case 101:
                                device.state = 1;
                                break;
                            case 2://暂停
                                device.state = 3;
                                break;
                            case 3://设备待出酒
                                device.state = 2;
                                break;
                            default:
                                break;
                        }
                        device.updatetime = DateTime.Now;
                        int oow = request.state == 1 ? request.oow : 0;
                        device.currentcapacity -= oow;
                        await _idevicetableRepository.EditDeviceState(device, oow, cancellationToken);
                        Console.WriteLine($"回调参数 设备数据更新成功！！！");

                        if(request.state == 101)
                        {
                            return new Json<string>() { Status = 1000, Detail = "操作成功" };
                        }

                       var orderInfo = await _iordertableRepository.getOrderTalbeByDeviceid(device.id, cancellationToken);
                        if (orderInfo != null && orderInfo.id > 0)
                        {
                            if (request.state == 1)
                            {
                                if (orderInfo.currentcapacity + request.oow == orderInfo.ordercapacity)
                                {
                                    orderInfo.state = 1;
                                    orderInfo.currentcapacity = request.oow;
                                    orderInfo.operationstate = 2;
                                }
                                else {
                                    orderInfo.state = 3;
                                }
                            }
                            else if (request.state == 2)
                            {
                                orderInfo.currentcapacity = request.oow;
                                orderInfo.operationstate = 1;
                            }
                            else if (request.state == 3)
                            {
                                orderInfo.state = 0;
                                orderInfo.operationstate = 3;
                            }
                            else if (request.state == 0 || request.state == 100)
                            {
                                orderInfo.state = 3;
                            }
                            orderInfo.updatetime = DateTime.Now;
                            await _iordertableRepository.EditOrderTableState(orderInfo, cancellationToken);
                            return new Json<string>() { Status = 1000, Detail = "操作成功" };
                        }
                        else {
                            return new Json<string>() { Status = 1000, Detail = "未找到需要更新的订单" };
                        }
                    }
                    else {
                        return new Json<string>() { Status = 1000, Detail = "未找到当前状态" };
                    }
                }
                else
                {
                    return new Json<string>() { Status = 1003, Detail = "未找到当前设备" };
                }
            }
            else
            {
                return new Json<string>() { Status = 1003, Detail = "当前参数暂不使用" };
            }




        }
    }
}
